package com.mingte.manghe.service.impl;

import com.mingte.manghe.config.RabbitMqConfig;
import com.mingte.manghe.dto.MailDTO;
import com.mingte.manghe.service.MailService;
import com.mingte.manghe.utils.MailUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：luxinsheng
 * @Email ：282860642@qq.com
 * @date ：Created in 2021/12/15 12:02
 * @description：邮件发送实现类
 * @modified By：
 * @version: $
 */
@Service
public class MailServiceImpl implements MailService {

    private Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailUtils mailUtils;

    @Override
    public int sendMsgToMq(String msg) {
        //消息生产
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_A, RabbitMqConfig.ROUTINGKEY_B, msg);
        logger.info("rabbitmq消息已经发送到交换机, 等待交换机接收..." + msg);
        return 1;
    }

    @RabbitListener(queues = RabbitMqConfig.QUEUE_B)
    private void sendEMail(String msg) {
        MailDTO mailDTO = new MailDTO();
        mailDTO.setSubject("测试");
        mailDTO.setText(msg);
        //发送邮件
        mailUtils.sendMail(mailDTO);
        logger.info("已发送邮件");
    }
}
